<!DOCTYPE html>
<html lang="zh">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>注册/忘记密码</title>
</head>

<body>

	<div id="app">
		<div>
			注册<input type="radio" :value="true" v-model="reg">
			忘记密码<input type="radio" :value="false" v-model="reg">
		</div>

		手机号<input type="radio" value="phone" v-model="picked">
		邮箱<input type="radio" value="email" v-model="picked">
		<br>
		{{picked=='phone'?'手机号':'邮箱'}}:
		<input v-model="o.openid">
		<br>
		验证码：<input v-model="o.code">
		<button @click="send" :disabled="o.disabled">发送验证码</button>
		<br>
		密码：<input type="password" v-model="o.password">
		<br>
		重复密码：<input type="password" v-model="o.password1">
		<br>
		<button @click="submit">提交</button>
	</div>

	<script src="/scriptZIP/vue.min.js"></script>
	<script src="/scriptZIP/axios.min.js"></script>

	<script src="/js/common.js"></script>

	<script>
		new Vue({
			el: '#app',
			data: {
				reg: true,
				picked: 'phone',
				o: {}
			},
			methods: {
				send: function () {
					var func = function (res) {
						var a = res.data.code;
						if (a == 0) {
							alert('已发送验证码');
							return;
						}
						if (a == 1) {
							alert('该账户已注册，请登录');
							return;
						}
					};

					if (this.picked == 'phone') {
						if (!/^1[3-9]\d{9}|6\d{7}$/.test(this.o.openid)) {
							alert('这似乎不是一个有效的手机号码');
							return;
						}
						this.o.disabled = true;
						axios.post('/api/reg/phone/' + this.o.openid,
							toFormParams({ reg: this.reg })).then(func);
					} else {
						if (!/^\S+@\S+\.\S+$/.test(this.o.openid)) {
							alert('这似乎不是一个有效的邮箱');
							return;
						}
						this.o.disabled = true;
						axios.post('/api/reg/email', toFormParams({
							email: this.o.openid,
							reg: this.reg
						})).then(func);
					}
				},
				submit: function () {
					if (!this.o.code) {
						alert('请输入验证码');
						return;
					}
					if (!this.o.password) {
						alert('请输入密码');
						return;
					}
					if (this.o.password != this.o.password1) {
						alert('密码不一致');
						return;
					}
					axios.post('/api/reg/app', toFormParams({
						code: this.o.code,
						password: this.o.password
					})).then(function (res) {
						location.replace('index2.html');
					});
				}
			},
			watch: {
				picked: function (nv, ov) {
					this.o.openid = '';
				}
			}
		});

	</script>

</body>

</html>